/*
** prompt.c for 42sh in /home/demesy_j/svn/quarante-deuxsh/trunk/src
** 
** Made by julien demesy
** Login   <demesy_j@epitech.net>
** 
** Started on  Tue Mar 13 11:48:20 2012 julien demesy
** Last update Thu Mar 15 16:27:06 2012 julien demesy
*/

#include	<stdlib.h>
#include	<stdio.h>
#include	<string.h>
#include	<sys/types.h>
#include	"builtins.h"

void		welcome()
{
  FILE		*fp;
  char		*line;
  size_t	len;
  ssize_t	read;

  line = NULL;
  len = 0;
  fp = fopen("prompt", "r");
  if (fp != NULL)
    {
      while ((read = getline(&line, &len, fp)) != -1)
	  printf("%s", line);
      free(line);
      fclose(fp);
    }
}

static	char	*get_pwd(t_list *env)
{
  char		*ret;
  char		*pwd;
  int		i;
  int		j;

  pwd = my_getenv(env, "PWD");
  if (pwd == NULL)
    return (NULL);
  i = strlen(pwd);
  while (pwd[i - 1] != '/' && i >= 0)
    i--;
  ret = malloc((strlen(pwd) - i) * sizeof(char));
  if (ret == NULL)
    return (NULL);
  j = 0;
  while (pwd[i])
    ret[j++] = pwd[i++];
  ret[j] = '\0';
  return (ret);
}

char		*prompt(t_list *env)
{
  static char		*cmd;
  ssize_t		read;
  size_t		len;

  printf("[%s@%s %s]$ ", my_getenv(env, "USER"), my_getenv(env, "HOSTNAME"), get_pwd(env));
  len = 0;
  if ((read = getline(&cmd, &len, stdin)) == -1)
    my_exit(42);
  return (cmd);
}
